package com.jz.kufangkuqu.biaoshi.controller;


import com.jz.kufangkuqu.biaoshi.bean.BiaoShiAdd;
import com.jz.kufangkuqu.biaoshi.bean.BiaoShiSelect;
import com.jz.kufangkuqu.biaoshi.bean.BiaoShiUpdate;
import com.jz.kufangkuqu.biaoshi.bean.BiaoShiUpdatestatus;
import com.jz.kufangkuqu.biaoshi.service.BiaoShiService;
import com.jz.util.ResultBuildVO;
import com.jz.util.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;


@RestController
@CrossOrigin
public class BiaoShiController {

    @Autowired
    private BiaoShiService biaoShiService;

//    //状态下拉框
//    @PostMapping("iwm/getPullDownList")
//    public ResultVO queryZhuangTai(){
//        List<Status> statuses = new ArrayList<>();
//        for (StatusEnum statusEnum:StatusEnum.values()) {
//            Status status = new Status(statusEnum.getStatus(),statusEnum.getStatusName());
//            statuses.add(status);
//        }
//        return ResultBuildVO.success(statuses);
//    }

    //分页
    @PostMapping("warehouseTag/queryPage")
    public ResultVO querypage(@RequestBody BiaoShiSelect biaoShiSelectVo){
        if(biaoShiSelectVo.getPageIndex()==null){
            biaoShiSelectVo.setPageIndex(1);
        }
        if(biaoShiSelectVo.getPageSize()==null){
            biaoShiSelectVo.setPageSize(10);
        }
        ResultVO resultVO = biaoShiService.queryPage(biaoShiSelectVo);
        return resultVO;
    }

    //新增
    @PostMapping("warehouseTag/add")
    public ResultVO add(@RequestBody @Valid BiaoShiAdd biaoShiAdd, BindingResult bindingResult ){
        if (bindingResult.hasErrors()){
            return ResultBuildVO.error(bindingResult.getFieldError().getDefaultMessage());
        }
        ResultVO resultVO = biaoShiService.add(biaoShiAdd);
        return resultVO;
    }

    //修改
    @PostMapping("warehouseTag/update")
    public ResultVO update(@RequestBody @Valid BiaoShiUpdate biaoShiUpdate, BindingResult bindingResult ){
        if (bindingResult.hasErrors()){
            return ResultBuildVO.error(bindingResult.getFieldError().getDefaultMessage());
        }
        ResultVO resultVO = biaoShiService.update(biaoShiUpdate);
        return resultVO;
    }


    //启用禁用
    @PostMapping("warehouseTag/updateStatus")
    public ResultVO updateStatus(@RequestBody @Valid BiaoShiUpdatestatus biaoShiUpdatestatus, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuildVO.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return biaoShiService.updateStatus(biaoShiUpdatestatus);
    }
}